package gui;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import enums.CellType;

import application.Cell;
import application.GameManager;
import application.GameMatrix;
import application.snakes.Snake;

public class GamePanel extends JPanel{

	private GameMatrix _matrix;
	private Snake _snake;
	private int _size;
	private ImageIcon _snakeHead;
	private ImageIcon _snakeBody;
	private Image _backround;

	
	public GamePanel(GameMatrix matrix,Snake snake,ImageIcon[] pictures){
		
		this._matrix = matrix;
		this._size = this._matrix.get_dimension();
		this._snake = snake;
		if(pictures.length == 3){
			this._snakeHead = pictures[0];
			this._snakeBody = pictures[1];
			this._backround = pictures[2].getImage();
		}
		this.setSize(610,610);
	}
	
	public void paint(Graphics g) {
		//super.paint(g);
		Image offIm = createImage(getSize().width, getSize().height);
		Graphics offGr = offIm.getGraphics();
		offGr.drawImage(this._backround, 0, 0, null);
		for (int i = 0; i < this._matrix.get_dimension(); i++) {
			for(int j = 0; j < this._matrix.get_dimension();j++){
				Cell c = this._matrix.getCell(i, j);
				if(CellType.isSnake(c.get_type())){
					if(CellType.isSnakeHead(c.get_type())){
						//this._snake.get_headIm().paintIcon(this,offGr, i*10, j*10);
						this._snakeHead.paintIcon(this, offGr, i*20, j*20);
					}
					else if (CellType.isSnakeBody(c.get_type())){
						//this._snake.get_bodyIm().paintIcon(this,offGr, i*10, j*10);
						this._snakeBody.paintIcon(this,offGr, i*20, j*20);
					}
					else if(CellType.isSnakeTail(c.get_type())){
						//this._snake.get_tailIm().paintIcon(this, offGr, i*10, j*10);
					}
				}
				if(CellType.isFood(c.get_type())){
					c.set_im();
					c.getIcon().paintIcon(this, offGr, i*20, j*20);
					//g.setColor(Color.green);
					//g.fillOval(i*10,j*10, 20, 20);
					//g.setColor(Color.black);
				}//TODO: add for CellType.ICECREAM and CellType.INSECT
			}
		}
		g.drawImage(offIm, 0,0, this);
	}
}
